//    Copyright (C) Clear Office.  All rights reserved.
using System;
using System.IO.Packaging;
using Office.Core.Serialization;
using Office.Spreadsheet.Serialization;

namespace Office.Spreadsheet.PivotTable
{
    class WorksheetSource:IOfficeXmlSerializable
    {
        #region IOfficeXmlSerializable Members

        public void ReadXml(ReadContext context)
        {
            using (ReadContext c = context.Read("worksheetSource"))
            {
                Ref = c.GetOptionalString("ref");
                Name = c.GetOptionalString("name");
                Sheet = c.GetOptionalString("sheet");
                string id = c.GetOptionalString(OfficeConstants.RelationshipNamespace + "id");
                if (id != null)
                {
                    PackageRelationship relationship = c.Part.GetRelationship(id);
                    if (relationship.TargetMode == TargetMode.External)
                    {
                        this._targetUri = relationship.TargetUri;
                    }
                    else
                    {
                        using (ReadContext c2 = c.OpenRelatedContext(id, SpreadsheetConstants.SpreadsheetNamespaceUri))
                        {
                            PivotCacheRecords = c2.ReadElement<PivotCacheRecords>();
                        }
                    }
                }
            }
        }

        public void WriteXml(WriteContext context)
        {
            using (var c = context.Write("worksheetSource"))
            {
                c.SetOptionalString("ref", Ref);
                c.SetOptionalString("name", Name);
                c.SetOptionalString("sheet", Sheet);
                if (_targetUri != null)
                {
                    var relationship =  c.Part.CreateRelationship(_targetUri, TargetMode.External, SpreadsheetConstants.ExternalLinkRelationshipType);
                    c.SetString(OfficeConstants.RelationshipNamespace + "id", relationship.Id);
                }
                else if (this._pivotCacheRecords != null)
                {
                    throw new NotImplementedException();
                    /*
                    using (var c2 = c.CreateRelatedContext(c.Prefix, c.NamespaceUri, c.GetAvailableUri("/xl/pivotCacheRecords/pivotCacheRecords{0}.xml"), SpreadsheetConstants.SpreadsheetNamespaceUri))
                    {
                        PivotCacheRecords = c2.ReadElement<PivotCacheRecords>();
                    }*/
                }
            }
        }

        #endregion

        private string _ref;

        public string Ref
        {
            get { return _ref; }
            set { _ref = value; }
        }
        private string _name;

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
        private string _sheet;

        public string Sheet
        {
            get { return _sheet; }
            set { _sheet = value; }
        }
        private PivotCacheRecords _pivotCacheRecords;
        private Uri _targetUri;

        public PivotCacheRecords PivotCacheRecords
        {
            get { return _pivotCacheRecords; }
            set { _pivotCacheRecords = value; }
        }
	
	
	
    }
}
